package com.ruoyi.project.modules.usage.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.fileInfo.domain.FileInf;
import com.ruoyi.project.modules.fileInfo.service.IFileInfoService;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceObject.service.IInterfaceObjectService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.page.domain.Page;
import com.ruoyi.project.modules.role.domain.ParticipateRole;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.service.IUsageService;
import com.ruoyi.project.modules.userDataAuth.domain.UserDataAuth;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/usage/usage/new")
public class UsageNewController extends BaseController {

    private String prefix = "usage/usage/new";

    @Autowired
    private IUsageService usageService;

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    @Autowired
    private IFileInfoService fileInfoService;

    @Autowired
    private IInterfaceObjectService interfaceObjectService;

    @RequiresPermissions("usage:usage:view")
    @GetMapping()
    public String function() {
        return prefix + "/usage";
    }

    @RequiresPermissions("usage:usage:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(Usage usage) {
        usage.setUserId(ShiroUtils.getSysUser().getUserId());
        usage.setFlag(this.getFlag());
        List<Usage> list = usageService.selectUsageList(usage);
        return AjaxResult.success(list);
    }

    @PostMapping("/detail/{id}")
    @ResponseBody
    public AjaxResult detail(@PathVariable("id") Long id) {
        Usage usage = this.usageService.selectUsageById(id);
        return AjaxResult.success(usage);
    }

    @PostMapping("/bffImgList")
    @ResponseBody
    public AjaxResult bffImgList(@RequestParam Long usageId) {
        Long interfaceId = this.getInterfaceId(usageId);
        if (null == interfaceId || 0 == interfaceId) {
            return AjaxResult.success();
        }
        FileInf fileInf = new FileInf();
        fileInf.setObjectId(interfaceId);
        fileInf.setObjectType("1");
        List<FileInf> fileInfs = this.fileInfoService.selectFileInfoList(fileInf);
        return AjaxResult.success(fileInfs);
    }

    private Long getInterfaceId(Long usageId) {
        InterfaceObject interfaceObject = new InterfaceObject();
        interfaceObject.setObjectId(usageId);
        interfaceObject.setObjectType("3");
        List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
        if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
            return interfaceObjectList.get(0).getInterfaceId();
        }
        return null;
    }
}
